﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;

/// <summary>
/// CertificatePolicyModule 用來處理Call WS的共用Module
/// </summary>
public class CertificatePolicyModule : IHttpModule
{
  #region IHttpModule 成員

  public void Dispose()
  {
    //throw new NotImplementedException();
  }

  public void Init(HttpApplication context)
  {
    context.BeginRequest += context_BeginRequest;
  }

  void context_BeginRequest(object sender, EventArgs e)
  {
    SSLValidator.OverrideValidation();
    #if DEBUG            
    #endif
  }

  #endregion
}

/// <summary>
/// 設計用來驗證SSL的類別
/// </summary>
public static class SSLValidator
{
  private static bool OnValidateCertificate(object sender, X509Certificate certificate,
    X509Chain chain,
    SslPolicyErrors sslPolicyErrors)
  {
    return true;
  }

  public static void OverrideValidation()
  {
    ServicePointManager.ServerCertificateValidationCallback = OnValidateCertificate;
    ServicePointManager.Expect100Continue = true;
  }
}